package top.ffhome.concurrency.example.syncContainer;

import lombok.extern.slf4j.Slf4j;
import top.ffhome.common.annotation.NotThreadSafe;

import java.util.Iterator;
import java.util.Vector;

@Slf4j
@NotThreadSafe
public class VectorExample3 {
    private static void test1(Vector<Integer> vector) {
        for (Integer it : vector) {
            if (it.equals(3)) {
                vector.remove(it);
            }
        }
    }

    private static void test2(Vector<Integer> vector) {
        Iterator<Integer> iterator = vector.iterator();
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            if (next.equals(3)) {
                vector.remove(next);
            }
        }

    }

    // success
    private static void test3(Vector<Integer> vector) {
        for (int i = 0; i < vector.size(); i++) {
            if (vector.get(i).equals(3)) {
                vector.remove(i);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Vector<Integer> vector = new Vector<>();
        vector.add(1);
        vector.add(2);
        vector.add(3);
        test3(vector);
    }
}
